/**
 * 便签相关API接口
 */

import request from '@/utils/request'

// 便签接口
export const noteApi = {
  /**
   * 创建便签
   * @param data 便签数据
   */
  createNote(data: {
    title: string
    content?: string
    markdownContent?: string
    folderId?: number
    tags?: string[]
  }) {
    return request.post('/notes', data)
  },

  /**
   * 更新便签
   * @param noteId 便签ID
   * @param data 更新数据
   */
  updateNote(noteId: number, data: {
    title?: string
    content?: string
    markdownContent?: string
    folderId?: number
    isFavorite?: boolean
    reminderTime?: string
    tags?: string[]
  }) {
    return request.put(`/notes/${noteId}`, data)
  },

  /**
   * 删除便签
   * @param noteId 便签ID
   */
  deleteNote(noteId: number) {
    return request.delete(`/notes/${noteId}`)
  },

  /**
   * 获取便签列表
   * @param params 查询参数
   */
  getNotes(params: {
    folderId?: number
    search?: string
    tags?: string[]
    isFavorite?: boolean
    page?: number
    size?: number
  } = {}) {
    return request.get('/notes', { params })
  },

  /**
   * 获取便签详情
   * @param noteId 便签ID
   */
  getNoteDetail(noteId: number) {
    return request.get(`/notes/${noteId}`)
  },

  /**
   * 获取最近编辑的便签
   * @param limit 限制数量
   */
  getRecentNotes(limit: number = 10) {
    return request.get('/notes/recent', {
      params: { limit }
    })
  },

  /**
   * 获取收藏的便签
   * @param limit 限制数量
   */
  getFavoriteNotes(limit: number = 10) {
    return request.get('/notes/favorites', {
      params: { limit }
    })
  },

  /**
   * 切换便签收藏状态
   * @param noteId 便签ID
   */
  toggleFavorite(noteId: number) {
    return request.post(`/notes/${noteId}/toggle-favorite`)
  },

  /**
   * 移动便签到指定文件夹
   * @param noteId 便签ID
   * @param targetFolderId 目标文件夹ID
   */
  moveNote(noteId: number, targetFolderId: number | null) {
    return request.post(`/notes/${noteId}/move`, {
      targetFolderId
    })
  },

  /**
   * 复制便签
   * @param noteId 便签ID
   * @param newTitle 新标题
   */
  copyNote(noteId: number, newTitle?: string) {
    return request.post(`/notes/${noteId}/copy`, {
      newTitle
    })
  },

  /**
   * 获取便签统计信息
   */
  getNoteStats() {
    return request.get('/notes/stats')
  },

  /**
   * 设置便签提醒
   * @param noteId 便签ID
   * @param reminderTime 提醒时间
   */
  setReminder(noteId: number, reminderTime: string) {
    return request.post(`/notes/${noteId}/reminder`, {
      reminderTime
    })
  },

  /**
   * 取消便签提醒
   * @param noteId 便签ID
   */
  cancelReminder(noteId: number) {
    return request.delete(`/notes/${noteId}/reminder`)
  },

  /**
   * 搜索便签
   * @param params 搜索参数
   */
  searchNotes(params: {
    keyword: string
    page?: number
    size?: number
  }) {
    return request.get('/notes/search', { params })
  },

  /**
   * 导出便签为Markdown
   * @param noteId 便签ID
   */
  exportToMarkdown(noteId: number) {
    return request.get(`/notes/${noteId}/export/markdown`, {
      responseType: 'text'
    })
  },

  /**
   * 批量删除便签
   * @param noteIds 便签ID列表
   */
  batchDeleteNotes(noteIds: number[]) {
    return request.delete('/notes/batch', {
      data: { noteIds }
    })
  }
}

// 便签文件夹接口
export const noteFolderApi = {
  /**
   * 获取文件夹树
   */
  getFolderTree() {
    return request.get('/note-folders/tree')
  },

  /**
   * 创建文件夹
   * @param data 文件夹数据
   */
  createFolder(data: {
    name: string
    parentId?: number
  }) {
    return request.post('/note-folders', data)
  },

  /**
   * 更新文件夹
   * @param folderId 文件夹ID
   * @param data 更新数据
   */
  updateFolder(folderId: number, data: {
    name?: string
    parentId?: number
  }) {
    return request.put(`/note-folders/${folderId}`, data)
  },

  /**
   * 删除文件夹
   * @param folderId 文件夹ID
   */
  deleteFolder(folderId: number) {
    return request.delete(`/note-folders/${folderId}`)
  }
}

// 便签相关类型定义
export interface Note {
  id: number
  userId: number
  title: string
  content?: string
  markdownContent?: string
  folderId?: number
  isFavorite: boolean
  reminderTime?: string
  viewCount: number
  createdAt: string
  updatedAt: string
  folder?: NoteFolder
  tags?: Tag[]
  relatedFiles?: any[]
}

export interface NoteFolder {
  id: number
  userId: number
  name: string
  parentId?: number
  sortOrder: number
  createdAt: string
  updatedAt: string
  children?: NoteFolder[]
  noteCount?: number
}

export interface Tag {
  id: number
  userId: number
  name: string
  color: string
  type: 'file' | 'note'
  usageCount: number
  createdAt: string
  updatedAt: string
}

export interface NoteStats {
  totalCount: number
  favoriteCount: number
  reminderCount: number
}
